package tsg.shape;

import tsg.util.Dimension;
import tsg.util.Point;


public class Rectangle extends Shape
{
	/**
	 * Creates a Rectangle with x = y = width = height = -1
	 */
	public Rectangle()
	{
		
	}
	
	/**
	 * 
	 * @param dimension
	 * @param point
	 */
	public Rectangle(Point point, Dimension dimension)
	{
		super.setSize		(dimension);
		super.setPosition	(point);
	}
	
	/**
	 * 
	 * @param rectangle
	 */
	public Rectangle(Rectangle rectangle)
	{
		super.setX		(rectangle.getX());
		super.setY		(rectangle.getY());
		super.setWidth	(rectangle.getWidth());
		super.setHeight	(rectangle.getHeight());
	}
	
	
	/**
	 * 
	 * @param x
	 * @param y
	 * @param width
	 * @param height
	 */
	public Rectangle(int x, int y, int width, int height)
	{
		super.setX		(x);
		super.setY		(y);
		super.setWidth	(width);
		super.setHeight	(height);
	}
	

	
	/**
	 * 
	 * @param rectangle
	 * @return
	 */
	public boolean equals(Rectangle rectangle)
	{
		boolean result = false;
		
		if(	rectangle.getX() 		== super.getX() 		&&
			rectangle.getY() 		== super.getY()			&&
			rectangle.getWidth()	== super.getWidth()		&&
			rectangle.getHeight()	== super.getHeight()	)
		{
			result = true;
		}
		
		return result;
	}
	
	/**
	 * 
	 * @param rectangle
	 * @return
	 */
	public Rectangle getUnion(Rectangle rectangle)
	{		
		int x1 = super.getX();
		int y1 = super.getY();
		int x2 = super.getX() + super.getWidth();
		int y2 = super.getY() + super.getHeight();
		
		if(x1 > rectangle.getX())
		{
			x1 = rectangle.getX();
		}
		
		if(x2 < rectangle.getX() + rectangle.getWidth())
		{
			x2 = rectangle.getX() + rectangle.getWidth();
		}
		
		if(y1 > rectangle.getY())
		{
			y1 = rectangle.getY();
		}
		
		if(y2 < rectangle.getY() + rectangle.getWidth())
		{
			y2 = rectangle.getY() + rectangle.getWidth();
		}
		
		
		return new Rectangle(x1, y1, x2 - x1, y2 - y1);
	}
}
